#include "myhead.h"

int tcpsock;//套接字标识符

void *func(void *arg)
{
    char buf[200]="";
    while(1)
    {
        int ret = recv(tcpsock,buf,sizeof(buf),0);
        if(ret==-1)
        {
            perror("recv");
            return NULL;
        }
        else  if(ret==0)
        {
            exit(-1);
        }
        printf("%s\n",buf);
        
        char *c_buf=buf;
        int n = 0;
        char **c_p=malloc(sizeof(char *));
        c_p[0]=strtok(c_buf,"#");
        while(c_p[n]!=NULL)
        {
            c_p=realloc(c_p,(n+1)*(sizeof(char *)));
            printf("%s\n",c_p[n]);
            n++;
            c_p[n]=strtok(NULL,"#");
        }
        int i=0;
        printf("字符切割完毕\n");
        while(i!=n)
        {
            char *p=strtok(c_p[i],"@");
            printf("第%d个ip是%s\n",i,p);
            p=strtok(NULL,"@");
            printf("第%d个端口是%s\n",i,p);
            p=strtok(NULL,"@");
            printf("第%d个名字是%s\n",i,p);
            i++;
        }
    }
}

int main()
{
    tcpsock=socket(AF_INET,SOCK_STREAM,0);
    if(tcpsock==-1)
    {
        perror("创建套接字失败\n");
        return -1;
    }

    int opt = 1;
    //设置端口取消绑定限制
    setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    //设置ip和端口（不要小于1024)
    struct sockaddr_in bindaddr;//结构体里面要有ip和端口
    bindaddr.sin_family=AF_INET;//选择地址协议ipv4
    bindaddr.sin_addr.s_addr=INADDR_ANY;//ip
    srand(time(NULL));
    int ran_dom=10010+rand()%10;
    printf("随机数是%d\n",ran_dom);
    bindaddr.sin_port=htons(ran_dom);//端口

    //绑定套接字ip和端口
    int ret = bind(tcpsock,(struct sockaddr *)&bindaddr,sizeof(struct sockaddr_in));
    if(ret==-1)
    {
        perror("bind");
        return -1;
    }

    //设置要连接的ip和端口
    struct sockaddr_in conaddr;//结构体里面要有ip和端口
    conaddr.sin_family=PF_INET;//选择地址协议ipv4
    //conaddr.sin_addr.s_addr=inet_addr("192.168.72.52");//小转大：inet_addr inet_pton inet_aton
    //一般用inet_pton
    if (inet_pton(AF_INET, "192.168.72.52", &conaddr.sin_addr) != 1) 
    {
        /* 返回 0 表示格式错，-1 表示 errno 错 */
        perror("inet_pton");
        exit(1);
    }
    conaddr.sin_port=htons(10000);//端口
    ret = connect(tcpsock,(struct sockaddr *)&conaddr,sizeof(struct sockaddr_in));
    if(ret == -1)
    {
        perror("connect\n");
        return -1;
    }

    char name[20]="lwl";
    send(tcpsock,name,strlen(name),0);

    //创建两个线程
    pthread_t id1;
    pthread_create(&id1,NULL,func,NULL);

    while (1)
    {
        int n=0;
        printf("请选择你功能\n");
        scanf("%d",&n);
        switch (n)
        {
            case 1:
            {
                char ins_buf[10]="getlist";
                send(tcpsock,ins_buf,strlen(ins_buf),0);
                sleep(1);
            }
            break;
            case 2:
            {
                char ins_buf[10]="news";
                send(tcpsock,ins_buf,strlen(ins_buf),0);
                sleep(1);

                char mynews[100]="";
                printf("输入你的信息内容\n");
                scanf("%99s",mynews);
                printf("输入你的好友ip和端口号(@隔离)\n");
                char myip[50]="",myport[50]="";
                scanf("%49s@%49s",mynews);
                char all_new[500]="";
                sprintf(all_new,"news#%s@%s@%s",mynews,myip,myport);
                send(tcpsock,all_new,strlen(all_new),0);
                printf("你发送了%s\n",all_new);
            }
            break;

            default:
            {
                printf("没识别\n");
            }
            break;
        }
    }
    close(tcpsock);
    pthread_join(id1,NULL);
}